﻿using LpbPrj.Client.Models;
using Prism.Commands;
using Prism.Events;
using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows;

namespace LpbPrj.Client.ViewModels
{
    public class IolMasterLiteExamViewModel : NavigationBaseViewModel
    {
        public IolMasterLiteExamViewModel(IEventAggregator eventAggregator, Prism.Regions.IRegionManager regionManager, Prism.Services.Dialogs.IDialogService dialogService)
            : base(eventAggregator, regionManager, dialogService)
        {
            AMenuItem menu = new AMenuItem() { Label = "菜单", Type = 0 };
            MenuItems.Add(menu);

            menu.AddChildren(new AMenuItem() { Label = "Login", Code = "LoginView", Type = 1, Command = new DelegateCommand<string>(para => this.Menu(para)) });
            menu.AddChildren(new AMenuItem() { Label = "Introduce", Code = "IntroduceView", Type = 1, Command = new DelegateCommand<string>(para => this.Menu(para)) });
        }

        protected override void MainClickEvent(string p)
        {
            base.MainClickEvent(p);

            if (p.Equals("AddMenuItem"))
            {
                AMenuItem menu = new AMenuItem() { Label = "菜单2", Type = 0 };
                MenuItems.Add(menu);

                menu.AddChildren(new AMenuItem() { Label = "Login2", Code = "LoginView", Type = 1, Command = new DelegateCommand<string>(para => this.Menu(para)) });
                menu.AddChildren(new AMenuItem() { Label = "Introduce2", Code = "IntroduceView", Type = 1, Command = new DelegateCommand<string>(para => this.Menu(para)) });
            }

            if (p.Equals("AddTreeViewItem"))
            {
                for (int i = 0; i < 10; i++)
                {
                    TreeViewModel viewModel = new TreeViewModel();
                    viewModel.Parent = null;
                    viewModel.IsChecked = false;
                    viewModel.Name = $"{i}";
                    viewModel.Visibility = Visibility.Visible;

                    for (int j = 0; j < 10; j++)
                    {
                        TreeViewModel viewModelTemp = new TreeViewModel();
                        viewModelTemp.Parent = viewModel;
                        viewModelTemp.IsChecked = false;
                        viewModelTemp.Name = $"{i}-{j}";
                        viewModelTemp.Visibility = Visibility.Collapsed;
                        viewModel.Children.Add(viewModelTemp);
                    }

                    TreeViewItems.Add(viewModel);
                }

            }

            if (p.Equals("Wait"))
            {
                Worker_Run(() =>
                {
                    Thread.Sleep(5000);

                    App.Current.Dispatcher.Invoke(() =>
                    {
                        System.Diagnostics.Debug.WriteLine("Dispatcher 0");

                        try
                        {
                            AMenuItem menu = new AMenuItem() { Label = "菜单2", Type = 0 };
                            MenuItems.Add(menu);

                            menu.AddChildren(new AMenuItem() { Label = "Login2", Code = "LoginView", Type = 1, Command = new DelegateCommand<string>(para => this.Menu(para)) });
                            menu.AddChildren(new AMenuItem() { Label = "Introduce2", Code = "IntroduceView", Type = 1, Command = new DelegateCommand<string>(para => this.Menu(para)) });

                            //Thread.Sleep(10000);
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message);
                        }

                        System.Diagnostics.Debug.WriteLine("Dispatcher 1");
                        Thread.Sleep(500);
                    });
                    System.Diagnostics.Debug.WriteLine("Dispatcher 2");

                    Thread.Sleep(5000);
                    return 100;

                }, waitingTitle: "设备......");
            }

        }


        public ObservableCollection<AMenuItem> MenuItems { get; set; } = new ObservableCollection<AMenuItem>();
        public DelegateCommand<string> MenuCommand =>
            new Lazy<DelegateCommand<string>>(() => new DelegateCommand<string>(p =>
            {
                Menu(p);

            })).Value;
        private void Menu(string viewname)
        {
            MessageBox.Show(viewname);
        }


        public ObservableCollection<TreeViewModel> TreeViewItems { get; set; } = new ObservableCollection<TreeViewModel>();



    }
}
